package demo6;


import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;

public class test {
    public static void main(String[] args) throws SQLException {
        //1.找到数据库服务器
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test2?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1234");

        //2.与数据库建立连接
        Connection connection = dataSource.getConnection();

        //3.构造sql语句
        String sql = "insert into student values(1,\"张三\")";
        PreparedStatement statement = connection.prepareStatement(sql);

        //4.执行sql语句
        int n = statement.executeUpdate();
        System.out.println("n = " + n);
        //5.关闭连接释放资源
        statement.close();
        connection.close();
    }
}
